package com.springbootnetty.server.imp;

import com.springbootnetty.dto.InBody;
import com.springbootnetty.handler.BaseHandler;
import com.springbootnetty.jt808.BaseHead;
import com.springbootnetty.utils.Jt808Util;
import com.springbootnetty.utils.SpringBenUtil;
import com.springbootnetty.server.InServer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class InServerImpl implements InServer {



    @Resource
    private SpringBenUtil springBenUtil;


    /**
     * 1.数据校验
     * 2。解析数据头，分析协议类型，
     *
     * */

    @Override
    public byte[] handle(InBody inBody) {
        int xorCode = Jt808Util.getXorCode(inBody.getNewByte());
        boolean res = Jt808Util.getRes(inBody.getNewByte(), xorCode);
        if (!res){
            System.out.println("校验未通过");
            return new byte[]{0x11,0x11};
        }
        BaseHead msgHead = new BaseHead(inBody.getNewByte());
        BaseHandler bean = springBenUtil.getBean(msgHead.getMsgId());
        if (null==bean){
            System.out.println("没有找到该类型数据包");
            return new byte[]{0x22,0x22};
        }
        return bean.handle(inBody,msgHead);
    }

    @Override
    public void test() {
        System.out.println("test");
    }
}
